import { createI18n } from 'vue-i18n'
import { useI18n } from 'vue-i18n'
import { useRoute } from 'vue-router'
import en from '../locales/en.json'
import zh from '../locales/zh.json'

export default defineNuxtPlugin(({ vueApp }) => {
  // 获取语言配置 (先从url获取，再从cookie获取，最后获取本地浏览器)
  const route = useRoute()
  let lan = 'zh'
  if(route?.query?.lang) {
    lan = route?.query?.lang as string
  } else if(useCookie('lang').value) {
    lan = useCookie('lang').value as string;
  }
  const lang = useCookie('lang');
  lang.value = lan; // 设置 cookie 的值为 'en'
  
  const i18n = createI18n({
    legacy: false,
    globalInjection: true,
    locale: lan,
    messages: {
      en,
      zh
    }
  })
  vueApp.use(i18n)
})
